package ldh.im.dto.codec;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import ldh.im.ImCoder;
import ldh.im.dto.base.Dto;

public abstract class ByteBufImCoder<T extends Dto> implements ImCoder<T, ByteBuf> {

    public abstract void decode(T t, ByteBuf byteBuf);
    public abstract void encode(T t, ByteBuf byteBuf);

    @Override
    public T decode(ByteBuf byteBuf) {
        T t = create();
        decode(t, byteBuf);
        return t;
    }

    @Override
    public ByteBuf encode(T to) {
        ByteBuf byteBuf = Unpooled.buffer();
        encode(to, byteBuf);
        return byteBuf;
    }

    public abstract T create();
}
